package com.example.demo.controller;

import javax.annotation.Resource;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;
<%
var className = tb.className;
var priName = tb.pri[0]?under2Camel(tb.pri[0].COLUMN_NAME):"";
var priType = tb.pri[0]?under2Camel(tb.pri[0].JavaType):"";
%>
import <%-props.packageName%>.<%-props.servicePackage%>.<%-className%>Service;
import <%-props.packageName%>.<%-props.entityPackage%>.<%-className%>;

@Controller
@RequestMapping("<%-tb.camel%>")
public class <%-className%>Controller {

	@Resource
	private <%-className%>Service <%-tb.camel%>Service;
	
	<%if(!props.Simple){%>
	/**
	* 	查询所有记录
	*/
	@ResponseBody
	@GetMapping("all")
	public Object findAll() {
		try {
			return <%-tb.camel%>Service.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	* 	分页查询
	*/
	@ResponseBody
	@GetMapping("page")
	public Object findByPage(@RequestParam int size,@RequestParam  int num) {
		try {
			return <%-tb.camel%>Service.findByPage(num, size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	<%
		if(priName){
	%>
	/**
	* 	根据主键查询
	*/
	@ResponseBody
	@GetMapping("by_key")
	public Object findByPrimaryKey(@RequestParam <%-camel2Pascl(priType)%> <%-priName%>) {
		try {
			<%-className%> <%-tb.camel%> = <%-tb.camel%>Service.findByPrimaryKey(<%-priName%>);
			return <%-tb.camel%>;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
		
	}
	<%}%>

	/**
	* 	根据<%-className%>查询
	*/
	@ResponseBody
	@GetMapping("by_<%-tb.camel%>")
	public Object findByPojo(@RequestBody <%-className%> <%-tb.camel%>) throws Exception {
		try {
			List<<%-className%>> <%-tb.camel%><%-props.listName%> = <%-tb.camel%>Service.findByPojo(<%-tb.camel%>);
		return <%-tb.camel%><%-props.listName%>;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	* 	根据<%-className%>更新
	*/
	@ResponseBody
	@PostMapping("update_<%-tb.camel%>")
	public Object updateByPojo(@RequestBody <%-className%> <%-tb.camel%>) throws Exception {
		try {
			Boolean result = <%-tb.camel%>Service.updateByPojo(<%-tb.camel%>);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
		
	}
	/**
	* 	添加数据
	*/
	@ResponseBody
	@PostMapping("insert_<%-tb.camel%>")
	public Object insert(@RequestBody <%-className%> <%-tb.camel%>) throws Exception {
		try {
			Boolean result = <%-tb.camel%>Service.insert(<%-tb.camel%>);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
		
	}
	

	<%-include(extrucPath(path.join(dirname,"uni_controller.ejs")),{tb:tb,className:className})%>
	<%}%>
}
